// pages/Access/index.js

let app = getApp();
const HttpAjax = getApp().getHttpData;
import Toast from '../../components/dist/toast/toast';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    active2: 0,
    star:[],
    show:false,
    key:{},
    ALLdata:[],
    loadendtxt:'当前没绑定账套,去添加账套',
    ishaspep:true,//没账套
    ishascur: true,
    searchval:'45445',
    loadtxt: '数据加载中！'
  },
  cahngesearch(event){
    console.log(event.detail);
    this.setData({
      searchval: event.detail
    })
  },
  onSearch(event) {
    // const { key } = event.currentTarget.dataset;
    // console.log(event.detail.value)
    // this.setData({
    //   [key]: event.detail
    // });
    this.getDatasearch(this.data.searchval)
  },
  iconclick(i){
    // console.log(i.target.dataset.did);
    // console.log(i.target.dataset.key);
    let did = i.target.dataset.key.DoorID;
    let cid = i.target.dataset.key.CustomerID;
    let idx = i.target.dataset.did;
    this.data.star[idx] = !this.data.star[idx];
    // console.log(this.data.star)
    let data = [...this.data.ALLdata];
    data[idx].Activity = this.data.star[idx];
    console.log(data)
    this.setData({
      star: this.data.star
    })
    let that=this;
    // return
    HttpAjax(app.globalData.url + '/Door/DoorActivity', { DoorID: did, CustomerID: cid, isActivity: this.data.star[idx]}, 'post', app.globalData.headerCookie)
      .then(ress => {
        // Toast.clear();
        let res = ress.data;
        console.log(res);
        if (res.result == 1 && that.data.star[idx]) {
          Toast('置顶成功');
          let arrdata = []
          let ka;
          let arr = []
          for (let index = 0; index < data.length; index++) {
            console.log(data[index].Activity)
            if (index === idx) {
              ka = data[index];
              continue;
            }
            if (data[index].Activity) {
              arrdata.unshift(data[index])
            } else {
              arrdata.push(data[index])
            }
          }
          arrdata.unshift(ka)
          for (let i = 0; i < arrdata.length; i++) {
            if (arrdata[i].Activity) {
              arr.push(true)
            } else {
              arr.push(false)
            }

          }
          that.setData({
            ALLdata: arrdata,
            star: arr
          })
        }else{
          Toast('取消置顶成功');
          let arrdata = []
          let ka;
          let arr = []
          for (let index = 0; index < data.length; index++) {
            // console.log(data[index].Activity)
            if (data[index].Activity) {
              arrdata.unshift(data[index])
            } else {
              arrdata.push(data[index])
            }
          }
          // arrdata.unshift(ka)
          for (let i = 0; i < arrdata.length; i++) {
            if (arrdata[i].Activity) {
              arr.push(true)
            } else {
              arr.push(false)
            }

          }

          // console.log(arrdata)
          that.setData({
            ALLdata: arrdata,
            star: arr
          })
        }
        
      })
  },
  cellClick(e){
    // console.log(e.target.dataset.key);
    // this.setData({
    //   key: e.target.dataset.key
    // })
    // this.setData({
    //   show:!this.data.show
    // })
    let key = JSON.stringify(e.target.dataset.key)
    wx.navigateTo({
      url: `./itemDoor/index?key=${key}`,
    })
  },
  ClosePOP(){
    this.setData({
      show:!this.data.show
    })
  },
  /**
   * 开门
   */
  openPOP(e){
    console.log(e.target.dataset.did);
    let did = e.target.dataset.did
    let cid = e.target.dataset.cid;
    Toast.loading({
      mask: true,
      duration:0,
      message: '正在开门...'
    });
    HttpAjax(app.globalData.url + '/AsyncDoor/OpenDoor', { DoorID: did, CustomerID:cid }, 'post', app.globalData.headerCookie)
      .then(ress=>{
        Toast.clear();
        let res=ress.data;
        console.log(res);
        if (res.result==1){
          Toast.success('开门成功');
        }
        if (res.result == -2) {
          Toast.fail('无此门权限');
        }
        if (res.result == -3) {
          Toast.fail('门已离线，开门失败');
        }
        if (res.result == -4) {
          Toast.fail('查询不到此门的IP地址，此门没有在账套服务器绑定');
        }
        
      }).catch(err=>{
        Toast.clear()
      })
  },
  toaddcurm(){
    wx.navigateTo({
      url: '../bindCard/addcard/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData();
    this.setData({
      loadtxt: '数据加载中！'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // console.log(123)
    this.setData({
      loadtxt: '刷新加载中！'
    })
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    this.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log(456);
    let self=this;
    // setTimeout(()=>{
    //   self.setData({
    //     ALLdata: self.data.ALLdata+10
    //   })
    // },500)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getData(b=''){
    Toast.loading({
      mask: true,
      duration:0,
      message: this.data.loadtxt
    });
    let arr = []
    let arrdata = []
    let that = this;
    // let hascustomer=false
    // async function getdata(){
      
    this.setData({
      loadendtxt:''
    })


    setTimeout(()=>{
      HttpAjax(app.globalData.url + '/Door/GetCustomerList', {}, 'post', app.globalData.headerCookie)
        .then(datas => {
          let rest = datas.data;
          if (rest.result == 1) {
            if (rest.customerlist.length >= 1) {
              HttpAjax(app.globalData.url + '/Door/GetDoorList', { DoorName: b }, 'post', app.globalData.headerCookie)
                .then(ress => {
                  // console.log(ress);
                  Toast.clear();
                  let res = ress.data;
                  if (res.result === 1) {
                    if (res.doorlist.length === 0) {
                      // that.data.ishaspep==true;
                      that.setData({
                        ishaspep: false,
                        loadendtxt: '暂无开门权限',
                        ishascur: false
                      })
                    } else {
                      that.setData({
                        ishaspep: true,
                        ishascur: false
                      })
                      let data = res.doorlist
                      for (let index = 0; index < data.length; index++) {
                        // console.log(data[index].Activity)
                        if (data[index].Activity) {
                          arrdata.unshift(data[index])
                        } else {
                          arrdata.push(data[index])
                        }
                      }
                      // arrdata.unshift(ka)
                      console.log(arrdata)
                      for (let i = 0; i < arrdata.length; i++) {
                        if (arrdata[i].Activity) {
                          arr.push(true)
                        } else {
                          arr.push(false)
                        }

                      }
                    }
                    that.setData({
                      ALLdata: arrdata,
                      star: arr
                    });
                    wx.stopPullDownRefresh();
                    wx.hideNavigationBarLoading();
                    console.log(that.data)
                  }
                  if (res.result === -1){
                    wx.navigateTo({
                      url: '../login/index',
                    })
                  }

                })
                .catch(err => {
                  Toast.clear()
                  console.log(err)
                })
            } else {
              Toast.clear();
              that.setData({
                ishaspep: false,
                loadendtxt: '当前没绑定账套,去添加账套',
                ishascur: true
              })
              wx.stopPullDownRefresh();
              wx.hideNavigationBarLoading();
            }
          }

        }).catch(err => {
          Toast.clear()
        })
    }, 500)
      
    
  },
  getDatasearch(b = ''){
    let that=this;
    Toast.loading({
      mask: true,
      duration: 0,
      message: '数据加载中'
    });
    let arrdata = []
    let arr = []
    setTimeout(() => {
              HttpAjax(app.globalData.url + '/Door/GetDoorList', { DoorName: b }, 'post', app.globalData.headerCookie)
                .then(ress => {
                  // console.log(ress);
                  Toast.clear();
                  let res = ress.data;
                  if (res.result === 1) {
                    if (res.doorlist.length === 0) {
                      // that.data.ishaspep==true;
                      that.setData({
                        ishaspep: false,
                        loadendtxt: '你没有权限,请前往一卡通云平台添加门权限',
                        ishascur: false
                      })
                    } else {
                      that.setData({
                        ishaspep: true,
                        ishascur: false
                      })
                      let data = res.doorlist
                      for (let index = 0; index < data.length; index++) {
                        // console.log(data[index].Activity)
                        if (data[index].Activity) {
                          arrdata.unshift(data[index])
                        } else {
                          arrdata.push(data[index])
                        }
                      }
                      // arrdata.unshift(ka)
                      console.log(arrdata)
                      for (let i = 0; i < arrdata.length; i++) {
                        if (arrdata[i].Activity) {
                          arr.push(true)
                        } else {
                          arr.push(false)
                        }

                      }
                    }
                    that.setData({
                      ALLdata: arrdata,
                      star: arr
                    });
                    wx.stopPullDownRefresh();
                    wx.hideNavigationBarLoading();
                    console.log(that.data)
                  }

                })
                .catch(err => {
                  Toast.clear()
                  console.log(err)
                })
            
    }, 500)
  }
  // onChange(event){
  //   console.log(event)
  //   this.setData({
  //     activeNames: event.detail
  //   });
  // }
})